package com.ews.onlineexchange.service.comparator;

import java.math.BigDecimal;
import java.util.Comparator;

import com.ews.onlineexchange.model.Trade;

public class AverageComparator implements Comparator<Trade> {
	
	private BigDecimal average;

	@Override
	public int compare(Trade o1, Trade o2) {		
		int compareTo = o1.getOffer().subtract(average).abs().compareTo(o2.getOffer().subtract(average).abs());
		if (compareTo != 0)
			return compareTo;
		else {
			return new PriceComparator().compare(o1, o2);			
		}
	}

	public AverageComparator(BigDecimal average) {
		this.average = average;
	}

}
